#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Author:Lijiacai
Email:1050518702@qq.com
===========================================
CopyRight@JackLee.com
===========================================
"""

import datetime
import hashlib
import time
import base64
import os
import re
import sys
import json
import pandas
import chardet
import graphene
import config
from common.view import BaseApi
from res.lijiacai.utils import tool


class Login(BaseApi, tool.RandomString):
    name = "Login"
    description = "登录"

    class Argument:
        UserName = graphene.String(description="账户名称", required=True)
        Password = graphene.String(description="账户密码", required=True)

    class Return:
        class FunctionAuth(graphene.ObjectType):
            Uri = graphene.String(description="路由")
            isAuth = graphene.Boolean(description="是否授权")

        class DataAuth(graphene.ObjectType):
            cityName = graphene.String(description="城市名称")
            isAuth = graphene.Boolean(description="是否授权")

        token = graphene.String(description="认证token")
        FunctionAuthRows = graphene.List(FunctionAuth, description="功能权限列表")
        DataAuthRows = graphene.List(DataAuth, description="数据权限列表")
        UserName = graphene.String(description="用户名")
        Password = graphene.String(description="密码")

    def validate_privilege(self, token_info, **kwargs):
        pass

    def deal_token(self, token):
        pass

    def deal(self, token_info, prilivege_info, **kwargs):
        UserName = self.arguments.get("condition", {}).get("UserName")
        Password = self.arguments.get("condition", {}).get("Password")
        for i in config.User:
            if UserName == i.get("UserName") and Password == i.get("Password"):
                data = {"UserName": UserName,
                        "Password": Password,
                        "Company": "PAND"}
                role = i.get("Auth")
                functionAuth = config.RoleAuth.get(role).get("functionAuth")
                dataAuth = config.RoleAuth.get(role).get("dataAuth")
                if not functionAuth or not dataAuth:
                    raise Exception("用户无权限")
                FunctionAuthRows = []
                DataAuthRows = []
                for m in range(len(functionAuth)):
                    if functionAuth[m] == True:
                        FunctionAuthRows.append({"Uri": config.functionTree[m], "isAuth": functionAuth[m]})
                for m in range(len(dataAuth)):
                    if dataAuth[m] == True:
                        DataAuthRows.append({"cityName": config.cityTree[m], "isAuth": dataAuth[m]})
                data.update({"FunctionAuthRows": FunctionAuthRows, "DataAuthRows": DataAuthRows})
                res = self.get_base64(json.dumps(data).encode("utf8")).decode("utf8")
                return {"token": res, "FunctionAuthRows": FunctionAuthRows, "DataAuthRows": DataAuthRows,
                        "UserName": UserName, "Password": Password}
        raise Exception("登录失败")
